﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SDF.Web.UI
{
    /// <summary>
    /// 
    /// 参考源：CodeIgniter Input输入类
    /// </summary>
    public class InputHelper
    {

        #region GET请求
        /// <summary>
        /// 根据键名获取请求(Request)的相应参数值(数字型)
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="defaultValue">转换失败时返回的默认值</param>
        /// <returns></returns>
        public int Get(string key, int defaultValue)
        {
            string text = HttpContext.Current.Request.QueryString[key];

            int result;
            bool flag = int.TryParse(text, out result);
            return flag ? result : defaultValue;
        }

        /// <summary>
        /// 根据键名获取请求(Request)的相应参数值(字符串型)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(string key)
        {
            string text = HttpContext.Current.Request.QueryString[key];
            if (string.IsNullOrEmpty(text))
                return string.Empty;

            return FilterSql(text);
        }

        /// <summary>
        /// 根据键名获取请求(Request)的相应参数值(字符串型)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(string key, string defaultValue)
        {
            string text = HttpContext.Current.Request.QueryString[key];
            if (string.IsNullOrEmpty(text))
                return defaultValue;

            return FilterSql(text);
        }



        public int QueryString(string key, int defaultValue)
        {
            return this.Get(key, defaultValue);
        }

        public string QueryString(string key)
        {
            return this.Get(key);
        }



        #endregion

        #region POST请求
        public string Post(string key)
        {
            string text = HttpContext.Current.Request.Form[key];
            if (string.IsNullOrEmpty(text))
                return string.Empty;

            return FilterSql(text);
        }

        public string Form(string key, string defaultValue)
        {
            string text = HttpContext.Current.Request.Form[key];
            if (string.IsNullOrEmpty(text))
                return defaultValue;

            return FilterSql(text);
        }

        public int Form(string key, int defaultValue)
        {
            string text = HttpContext.Current.Request.Form[key];
            int result;
            bool flag = int.TryParse(text, out result);
            return flag ? result : defaultValue;
        }


        public string Form(string key)
        {
            return this.Post(key);
        }


        #endregion




        public string FilterSql(string s)
        {
            //like子句通配符字符进行转义
            s = s.Replace("[", "[[]");
            //s = s.Replace("%", "[%]");
            //s = s.Replace("_", "[_]");


            s = s.Replace("'", "");
            return s;
        }




        public string FilterXss(string s)
        {
            //TODO:执行Xss过滤
            //.net 2.0使用第三方类库

            //.net 3.5+使用 AntiXss

            return s;
        }
    }
}